package com.able.concurrency.t1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abel.he
 * @data 2018/8/15 19:35
 * @describe volatile 只能保证可见性，不能保证原子性
 */
public class Test10 {

    volatile int num = 0;

    synchronized void m1() {
        for (int i = 0; i < 10000; i++) {
            num++;
        }
    }

    public static void main(String[] args) {
        List<Thread> list = new ArrayList<Thread>(10);
        final Test10 test10 = new Test10();
        for (int i = 0; i < 10; i++) {
            list.add(new Thread(new Runnable() {
                public void run() {
                    test10.m1();
                }
            }));
        }

        for (Thread thread :
                list) {
            thread.start();
        }
        for (Thread thread :
                list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(test10.num);
    }

}
